package com.jkgl.web.controller.system;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.jkgl.common.constant.Constants;
import com.jkgl.common.core.domain.AjaxResult;
import com.jkgl.common.core.domain.entity.SysMenu;
import com.jkgl.common.core.domain.entity.SysUser;
import com.jkgl.common.core.domain.model.LoginBody;
import com.jkgl.common.core.domain.model.LoginUser;
import com.jkgl.common.utils.SecurityUtils;
import com.jkgl.framework.web.service.SysLoginService;
import com.jkgl.framework.web.service.SysPermissionService;
import com.jkgl.framework.web.service.TokenService;
import com.jkgl.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author w020304m
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        SetOperations setOperations = redisTemplate.opsForSet();
        String todayKey = "login:count:"+LocalDate.now();
        setOperations.add(todayKey, loginBody.getUsername());
        // 设置集合在第七天的午夜过期
        LocalDateTime expireTime = LocalDate.now().plusDays(7).atStartOfDay();
        ZoneId zone = ZoneId.systemDefault();
        Date expireDate = Date.from(expireTime.atZone(zone).toInstant());

        // 设置过期时间
        redisTemplate.expireAt(todayKey, expireDate);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        if (!loginUser.getPermissions().equals(permissions))
        {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
